const Koa = require('koa');
const app = new Koa();
const mongo = require('./lib/mongo');
const router = require('./routes/user');
const serve = require('koa-static');
const path = require('path')
const { historyApiFallback } = require('koa2-connect-history-api-fallback');

app.use(historyApiFallback());
app.use(serve(path.join(__dirname)+'/public/'))


mongo.init()
app.use(async (ctx, next)=> {
    ctx.set('Access-Control-Allow-Origin', '*');
    ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    if (ctx.method == 'OPTIONS') {
      ctx.body = 200; 
    } else {
      await next();
    }
  });
// console.log('routes', router)

app.use(router.routes())

console.log('running port 3000')
app.listen(3000);
